package com.ff.neuropp.service;


import com.ff.neuropp.domain.Career;
import com.ff.neuropp.repository.CareerRepository;
import com.ff.neuropp.util.SpecificationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class CareerService {


    @Resource
    private CareerRepository careerRepository;


    /**
     * 创建实体
     *
     * @param career
     */
    public Career save(Career career) {
        return careerRepository.save(career);
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public Career findById(Integer id) {
        Optional<Career> optional = careerRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

    /**
     * 根据id 删除实体
     * @param id
     */
    public void deleteById(Integer id) {
        careerRepository.deleteById(id);
    }

    /**
     * 分页条件查询
     *
     * @param career
     * @return
     */
    public Page<Career> findPage(Career career) {
        Map<String, Object> sqlParams = new HashMap<>();
        if (null == career.getPageNo()) {
            career.setPageNo(1);
        } else if (career.getPageNo() < 1) {
            career.setPageNo(1);
        }
        if (null == career.getPageSize()) {
            career.setPageSize(10);
        }
        sqlParams.put("GT_id",0);
        if (null != career.getCareerType()){
            sqlParams.put("EQ_careerType.id",career.getCareerType().getId());
        }
        if(null !=career.getSearch() && career.getSearch().trim().length()>0){
            sqlParams.put("LIKE_careerName",career.getSearch());
        }
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = new PageRequest(career.getPageNo()-1,career.getPageSize(), direction, sort);
        return careerRepository.findAll(SpecificationUtil.buildSpecification(sqlParams, Career.class), pageable);
    }

    /**
     * 根据条件查询List
     */
    public List<Career> findList(Map<String, Object> param) {
        return careerRepository.findAll(SpecificationUtil.buildSpecification(param, Career.class));
    }

}
